package host.haiwai.controller;


import com.alibaba.fastjson.JSON;
import host.haiwai.entity.Menu;
import host.haiwai.entity.Role;
import host.haiwai.mapstruct.MenuListMapstruct;
import host.haiwai.mapstruct.MenuMapstruct;
import host.haiwai.mapstruct.MenuTreeMapstruct;
import host.haiwai.mapstruct.RoleMapstruct;
import host.haiwai.service.MenuServer;
import host.haiwai.vo.MenuListVo;
import host.haiwai.vo.MenuTreeVo;
import host.haiwai.vo.MenuVo;
import host.haiwai.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author joefche / JC
 * @since 2021-08-29
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuServer menuServer;

    /**
     * 后台首页左菜单
     * @return
     */
    @GetMapping("/menuTree")
    @ResponseBody
    public Object menuTree() {
        List<Menu> menus = menuServer.listWithTree();
        List<MenuVo> menuVos = MenuMapstruct.mapper.menuList2Vo(menus);

        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("title","首页");
        map1.put("href","/page/welcome.html");

        HashMap<Object, Object> map2 = new HashMap<>();
        map2.put("title","JC CURD");
        map2.put("image","/images/jc-logo.png");

        HashMap<Object, Object> map = new HashMap<>();
        map.put("homeInfo",map1);
        map.put("logoInfo",map2);
        map.put("menuInfo",menuVos);

        return JSON.toJSON(map);
    }

    /**
     * 列表页面显示
     * @return
     */
    @GetMapping("/menuList")
    public String menuList() {
        return "/menu/menuList";
    }

    /**
     * 列表json内容加载
     * @param page
     * @param limit
     * @return jsonObject
     */
    @RequestMapping("/jsonList")
    @ResponseBody
    public Object jsonList(@RequestParam(defaultValue = "1") Integer page ,
                           @RequestParam(defaultValue = "1") Integer limit) {
        List<Menu> menuList = menuServer.list();
        List<MenuListVo> menuVoList = MenuListMapstruct.mapper.menuListList2Vo(menuList);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        //map.put("count",menu.getTotal());
        map.put("data",menuVoList);
        return JSON.toJSON(map);

    }

    /**
     * 显示新增页面
     * @return
     */
    @GetMapping("/menuAdd")
    public ModelAndView menuAdd(ModelAndView modelAndView) {
        modelAndView.setViewName("/menu/menuAdd");
        return modelAndView;
    }

    @PostMapping("/menuAdd")
    @ResponseBody
    public Object menuAdd(Menu menu) {
        HashMap<Object, Object> map = new HashMap<>();
        boolean save = menuServer.save(menu);
        if(save) {
            map.put("code",1);
            map.put("data","操作成功");
        } else {
            map.put("code", 2);
            map.put("data", "操作失败");
        }
        return JSON.toJSON(map);
    }

    //删除操作
    @GetMapping("/deleteMenu")
    @ResponseBody
    public Object deleteRole(@RequestParam(defaultValue = "0") Integer id) {
        HashMap<Object, Object> map = new HashMap<>();
        boolean remove = menuServer.removeById(id);
        if(remove) {
            map.put("code",1);
            map.put("data","操作成功");
        } else {
            map.put("code", 2);
            map.put("data", "操作失败");
        }
        return JSON.toJSON(map);
    }

    //修改页面
    @GetMapping("/menuEdit")
    public ModelAndView roleEdit(@RequestParam(defaultValue = "0") Integer id, ModelAndView modelAndView) {
        modelAndView.addObject("id",id);
        modelAndView.setViewName("/menu/menuEdit");
        return modelAndView;
    }
    //更新会员资料
    @PostMapping("/menuEdit")
    @ResponseBody
    public Object menuEdit(Menu menu) {
        HashMap<Object, Object> map = new HashMap<>();

        boolean update = menuServer.updateById(menu);

        if(update) {
            map.put("code",1);
            map.put("data","操作成功");
        } else {
            map.put("code", 2);
            map.put("data", "操作失败");
        }
        return JSON.toJSON(map);
    }

    //获取指定资料
    @GetMapping("/getMenuById")
    @ResponseBody
    public Object getUserById(@RequestParam(defaultValue = "0") Integer id) {
        Menu menu = menuServer.getById(id);
        MenuListVo menuListVo = MenuListMapstruct.mapper.menuListPojo2Vo(menu);
        return JSON.toJSON(menuListVo);
    }

    @GetMapping("/layerTree")
    @ResponseBody
    public Object menuRoleJsonList() {
        List<Menu> menu = menuServer.listWithTree();
        List<MenuTreeVo> menuLayerList2Vo = MenuTreeMapstruct.mapper.menuLayerTree2Vo(menu);
        return JSON.toJSON(menuLayerList2Vo);
    }


}

